package com.ruoyi.sale.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sale.domain.SaleRevenue;
import com.ruoyi.sale.service.ISaleRevenueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售额Controller
 *
 * @author szm
 * @date 2025-11-15
 */
@Api(tags = "销售额明细")
@RestController
@RequestMapping("/sale/revenue")
public class SaleRevenueController extends BaseController {
    @Autowired
    private ISaleRevenueService saleRevenueService;

    /**
     * 查询销售额列表
     */
    @ApiOperation("查询销售额列表")
    @GetMapping("/list")
    public TableDataInfo list(SaleRevenue entity, HttpServletRequest req) {
        startPage();
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(entity.getQueryStart())){
            wrapper.between("apply_date",entity.getQueryStart(),entity.getQueryEnd());
        }
        wrapper.setEntity(entity);
        List<SaleRevenue> list = saleRevenueService.list(wrapper);

        List<SaleRevenue> listMoney = saleRevenueService.lambdaQuery().between(SaleRevenue::getApplyDate, entity.getQueryStart(), entity.getQueryEnd()).list();
        int sum = 0;
        if (listMoney.size() > 0){
            sum = listMoney.stream().mapToInt(SaleRevenue::getMoney).sum();
        }

        TableDataInfo dataInfo = getDataTable(list);
        dataInfo.setMoneys(sum);
        return dataInfo;
    }

    /**
     * 导出销售额列表
     */
    @ApiOperation("导出销售额列表")
    @Log(title = "销售额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleRevenue entity) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<SaleRevenue> list = saleRevenueService.list(wrapper);
        ExcelUtil<SaleRevenue> util = new ExcelUtil<SaleRevenue>(SaleRevenue. class);
        util.exportExcel(response, list, "销售额数据");
    }

    /**
     * 获取销售额详细信息
     */
    @ApiOperation("获取销售额详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") String id) {
        return success(saleRevenueService.getById(id));
    }

    /**
     * 新增销售额
     */
    @ApiOperation("新增/修改销售额")
    @Log(title = "销售额", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SaleRevenue entity) {
        return toAjax(saleRevenueService.saveOrUpdate(entity));
    }

    /**
     * 删除销售额
     */
    @ApiOperation("删除销售额")
    @Log(title = "销售额", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        return toAjax(saleRevenueService.removeById(ids));
    }
}
